package yxy.game.pm2.active.command.flop;

import com.alibaba.fastjson.JSONObject;
import yxy.apple.logger.Logger;
import yxy.apple.util.ArraryMesh;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actflopshop;
import yxy.cherry.data.bean.config;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopShop;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.player.FunctionHandle;

import java.util.Collection;
import java.util.Map;

/**
 * 翻牌 - 商城
 *
 * @author Dell
 */
@MessageType(proto = Command.ActFlopCardShopInfo, classtype = ActiveMessage.ActFlopCardShopInfoRequest.class)
public class CMD_Active_翻牌_商城_Info extends Command<ActiveMessage.ActFlopCardShopInfoRequest> {

    private static final Logger logger = Logger.create(CMD_Active_翻牌_商城_Info.class);

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardShopInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActFlopCardShopInfoResponse.Builder response = ActiveMessage.ActFlopCardShopInfoResponse.newBuilder();

        ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode, false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        if (!am.running()) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        //商城
        Map<String, actflopshop> afsmap = ActDataCenter.getAllData(actflopshop.class, playercode);

        //todo 相隔了几个月(动态增加商品数量 2022.08.26)
        Integer passMonth = FunctionHandle.getAlreadlyPassMonth(playercode);
        config data = DataCenter.getData(4, config.class);
        JSONObject jsonObject = JSONObject.parseObject(data.getValue());
        Integer maxraisetimes = jsonObject.getInteger("maxraisetimes");
        PlayerBean playerBean = PlayerBean.get(playercode);
        Integer begin_server = jsonObject.getInteger("begin_server");

        for (FlopShop fs : am.getFss()) {
            ActiveMessage.FlopShop.Builder build = ActiveMessage.FlopShop.newBuilder();

            build.setId(fs.getId());
            build.setGoodsid(fs.getGoodsid());
            build.setLimitnum(fs.getLimitnum());
            build.setLocknum(fs.getLock().size());
            build.setBuynum(fs.getBuy().size());

            actflopshop afs = afsmap.get(String.valueOf(fs.getId()));

            int[] num = playerBean.getServerId() >= begin_server ? ArraryMesh.getNum(afs.getShopnum(), afs.getRaise(), passMonth, maxraisetimes) : afs.getShopnum();

            Collection<Currency> curs = Currency.create(afs.getShoptype(), afs.getShopid(), num);
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);

            build.addAllCurs(items);

            int canget = DrawStatus.cannotget.getStatus();
            if (fs.getBuy().size() >= fs.getLimitnum()) {
                canget = DrawStatus.hadget.getStatus();
            } else {
                if (fs.getBuy().size() + fs.getLock().size() >= fs.getLimitnum()) {
                    canget = DrawStatus.cannotget.getStatus();
                } else {
                    canget = DrawStatus.canget.getStatus();
                }
            }
            build.setCanget(canget);

            response.addFss(build.build());
        }

        response.setStart(am.getTimeline().getStartTime().getMillis());
        response.setEnd(am.getTimeline().getEndTime().getMillis());
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActFlopCardShopInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
